package cn.tedu.lbs.basic.web.service;

import cn.tedu.lbs.basic.web.repository.AdminRepository;
import cn.tedu.lbs.basic.web.repository.UserGroupRepository;
import cn.tedu.lbs.basic.web.utils.AuthenticationContextUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.po.AdminPO;
import com.tarena.lbs.pojo.basic.po.UserGroupPO;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import com.tarena.lbs.pojo.basic.vo.UserGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserGroupService {
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Autowired
    private AdminRepository adminRepository;
    public PageResult<UserGroupVO> pageList(UserGroupQuery query) throws BusinessException {
        //1.获取登录用户id adminId
        Integer adminId = getUserId();
        //2.检查 并且获取businessId lbs_admin
        Integer businessId = checkAndGetBusinessId(adminId);
        //3.调用数据层返回数据
        List<UserGroupPO> pos= getUserGroups(businessId);
        //4.组织一个分页数据 objects
        PageResult<UserGroupVO> voPage=new PageResult<>(query);
        voPage.setTotal(100l);
        List<UserGroupVO> vos=null;
        if (pos!=null&&pos.size()>0){
            vos=pos.stream().map(po->{
                UserGroupVO vo=new UserGroupVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }

    private List<UserGroupPO> getUserGroups(Integer businessId) {
        //select * from lbs_user_group where business_id=#{businessId}
        QueryWrapper<UserGroupPO> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        return userGroupRepository.list(queryWrapper);
    }

    private Integer checkAndGetBusinessId(Integer adminId) throws BusinessException {
        AdminPO po = adminRepository.getById(adminId);
        Asserts.isTrue(po==null,new BusinessException("-2","商家账号不存在"));
        return po.getBusinessId();
    }

    private Integer getUserId() throws BusinessException {
        //读取 认证 传递的userPrinciple 线程和过滤器线程相同
        UserPrinciple userPrinciple = AuthenticationContextUtil.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
        return userPrinciple.getId();
    }

    public void addGroup(UserGroupParam param) throws BusinessException {
        //1.获取用户id 从而检查并获取需要关联的商家
        Integer adminId = getUserId();
        Integer businessId = checkAndGetBusinessId(adminId);
        //2.组织po
        UserGroupPO po=new UserGroupPO();
        BeanUtils.copyProperties(param,po);
        po.setBusinessId(businessId);
        po.setCreateAt(new Date());
        userGroupRepository.save(po);
    }

    public List<Integer> getUserGroupIds(Integer userId, Integer businessId) {
        return userGroupRepository.getUserGroupIds(userId,businessId);
    }
}
